/*
 * @Author: 姚潘涛
 * @Date: 2024-04-08 15:18:03
 * @LastEditors: 姚潘涛
 * @LastEditTime: 2024-10-10 15:22:37
 * @Description:
 *
 * Copyright (c) 2024 by pandaman, All Rights Reserved.
 */
#include "xict_cpp/core/pipeline/remote_controller.h"

namespace xict_cpp {
    namespace core {
        namespace pipeline {
            RemoteControllerPipeline::~RemoteControllerPipeline() {
                stop_flag_ = true;

                for (auto& thread : threads_) {
                    if (thread.joinable()) {
                        thread.join();
                    }
                }
            }

            bool RemoteControllerPipeline::Init() {
                return Init(RemoteControllerPipelineParams());
            }

            bool RemoteControllerPipeline::Init(
                const RemoteControllerPipelineParams& params) {
                params_ = params;
                return Initialize();
            }

            void RemoteControllerPipeline::Start() {
                stop_flag_ = false;
                threads_.clear();

                if (params_.switcher.enable_stream) {
                    threads_.emplace_back(
                        &RemoteControllerPipeline::RunStreamProcessor, this);
                }
                if (params_.switcher.enable_can) {
                    threads_.emplace_back(
                        &RemoteControllerPipeline::RunCanetProcessor, this);
                }
                if (params_.switcher.enable_heartbeat) {
                    threads_.emplace_back(
                        &RemoteControllerPipeline::RunHeartbeatSender, this);
                }
                if (params_.switcher.enable_draw_trajectory) {
                    threads_.emplace_back(
                        &RemoteControllerPipeline::RunDrawTrajectoryProcessor,
                        this);
                }
            }

            void RemoteControllerPipeline::Stop() {
                stop_flag_ = true;
                for (auto& thread : threads_) {
                    if (thread.joinable()) {
                        thread.join();
                    }
                }
            }

            std::string RemoteControllerPipeline::Name() const {
                return "RemoteControllerPipeline";
            }

            bool RemoteControllerPipeline::Initialize() {
                // 初始化 stream processor stage
                if (params_.switcher.enable_stream) {
                    stream_processor_stage_ = std::make_shared<
                        xict_cpp::core::stage::StreamProcessorStage>();
                    if (!stream_processor_stage_->Init(params_.stream)) {
                        AERROR_F("failed to init stream processor stage");
                        return false;
                    }
                }

                // 初始化 canet processor stage
                if (params_.switcher.enable_can) {
                    canet_processor_stage_ = std::make_shared<
                        xict_cpp::core::stage::CanetProcessorStage>();
                    if (!canet_processor_stage_->Init(params_.canet)) {
                        AERROR_F("failed to init canet processor stage");
                        return false;
                    }
                }
                // 初始化 heartbeat_sender_stage
                if (params_.switcher.enable_heartbeat) {
                    heartbeat_sender_stage_ = std::make_shared<
                        xict_cpp::core::stage::HeartbeatSenderStage>();
                    if (!heartbeat_sender_stage_->Init(params_.heartbeat)) {
                        AERROR_F("failed to init heartbeat sender stage");
                        return false;
                    }
                }
                // 初始化 draw_trajectory_stage
                if (params_.switcher.enable_draw_trajectory) {
                    draw_trajectory_processor_stage_ = std::make_shared<
                        xict_cpp::core::stage::DrawTrajectoryStage>();
                    if (!draw_trajectory_processor_stage_->Init(
                            params_.trajectories_params_list)) {
                        AERROR_F("failed to init draw trajectory stage");
                        return false;
                    }
                }

                return true;
            }

            bool RemoteControllerPipeline::InnerProcess() {
                return false;
            }

            void RemoteControllerPipeline::Reset() { }

            void RemoteControllerPipeline::RunStreamProcessor() {
                while (!stop_flag_) {
                    stream_processor_stage_->Process();
                }
            }

            void RemoteControllerPipeline::RunCanetProcessor() {
                while (!stop_flag_) {
                    canet_processor_stage_->Process();
                }
            }

            void RemoteControllerPipeline::RunHeartbeatSender() {
                while (!stop_flag_) {
                    heartbeat_sender_stage_->Process();
                }
            }

            void RemoteControllerPipeline::RunDrawTrajectoryProcessor() {
                while (!stop_flag_) {
                    draw_trajectory_processor_stage_->Process();
                }
            }
        }   // namespace pipeline
    }       // namespace core
};          // namespace xict_cpp
